package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.*;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseMapper;
import com.koron.css2.clientCenter.mapper.ValvePubMapper;
import com.koron.css2.clientCenter.mapper.ValveUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.swan.bean.MessageBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 添加关阀信息
 *
 * @author cd
 * @author modify by 杨嘉慧
 */
public class ValveCloseAdd implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValveCloseAddBean addBean = JsonUtils.objectToPojo(req.getData(), ValveCloseAddBean.class);
            if (StringUtils.isBlank(addBean.getPlanTime())) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "添加失败,计划时间有误", null);
            }
            if (StringUtils.isBlank(addBean.getReason())) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "添加失败,关阀原因为空", null);
            }

            //添加关阀主表信息
            ValveCloseBean valveCloseBean = new ValveCloseBean();
            BeanUtils.copyProperties(addBean, valveCloseBean);
            valveCloseBean.setReceiptTime(new Date());
//            valveCloseBean.setPlanStartTime(Tools.getDate(addBean.getPlanStartTime()));
            valveCloseBean.setPlanTime(Tools.getDate(addBean.getPlanTime()));
            String id = Tools.getObjectId();
            valveCloseBean.setId(id);
            valveCloseBean.setbillNo(Tools.getBillNo(factory, "valve_close_code"));
            valveCloseBean.setCreateName(userInfo.getUserInfo().getName());
            valveCloseBean.setUpdateName(userInfo.getUserInfo().getName());
            Date time = new Date();
            valveCloseBean.setCreateTime(time);
            valveCloseBean.setUpdateTime(time);
            ValveCloseMapper valveCloseMapper = factory.getMapper(ValveCloseMapper.class);
            valveCloseMapper.insert(valveCloseBean);

            //根据userNo添加关阀详情表信息
            List<ValveCloseDetailBean> userList = addBean.getUserNoList();

            ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
            List<String> userNoList = userList.stream().map(ValveCloseDetailBean::getUserNo).collect(Collectors.toList());
            List<MeterReaderBean> meterReaderList = valveUserMapper.selectMeterReaderList(userNoList);
            Map<String, MeterReaderBean> meterReaderBeanMap = meterReaderList.stream().collect(Collectors.toMap(MeterReaderBean::getUserNo, meterReaderBean -> meterReaderBean, (value1, value2) -> {
                return value2;
            }));

            //查询阀控类型
            ValvePubMapper pubMapper = factory.getMapper(ValvePubMapper.class);
            List<ValveMessageBean> valveMessageBeanList = pubMapper.getValveMessage();
            Map<String, ValveMessageBean> valveMap = valveMessageBeanList.stream().collect(Collectors.toMap(ValveMessageBean::getId, meterReaderBean -> meterReaderBean));


            //user_info.meter_no获取用户的阀控类型 valveControlFlag (MS_INFO.meter_model   PUB_METER_MODEL.valve_control) 和  远控类型 remoteFlag MS_INFO.meter_type=2
            //组装关阀单明细数据
            List<ValveCloseDetailBean> detailBeans = new ArrayList<>();
            for (ValveCloseDetailBean user : userList) {
                MeterReaderBean meterReaderBean = meterReaderBeanMap.get(user.getUserNo());
                ValveCloseDetailBean detailBean = user;
                detailBean.setId(Tools.getObjectId());
                detailBean.setUserNo(user.getUserNo());
                detailBean.setCreateTime(time);
                detailBean.setValveCloseId(valveCloseBean.getId());
                detailBean.setValveStatus("1");//关阀单明细数据初始化为待关阀
                ValveCloseUpdate.tobean(valveCloseBean, valveMap, detailBeans, meterReaderBean, detailBean);
            }

            //添加关阀单明细数据
            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
            detailMapper.insertBatch(detailBeans);

            MessageBean<HashMap> bean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", HashMap.class);
            HashMap<String, String> map = new HashMap<>();
            map.put("id", id);
            bean.setData(map);
            return bean;
        } catch (Exception e) {
            logger.error("添加失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "添加失败", null);
        }
    }

}
